package com.tanhua.app.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.tanhua.api.db.UserInfoApi;
import com.tanhua.api.mongo.CommentApi;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.UserCommentVo;
import com.tanhua.util.RelativeDateFormat;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentService {

    @Reference
    private CommentApi commentApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Autowired
    private QuanziMQService quanziMQService;

    public ResponseEntity findCommentVoByPage(Integer pageNum, Integer pageSize, String publishId) {
        // 1、调用api查询comment
        PageBeanVo pageBeanVo = commentApi.findByPage(pageNum, pageSize, publishId);

        // 2、 userInfo  + comment  = vo
        List<CommentVo> voList = new ArrayList<>();

        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                CommentVo vo = new CommentVo();

                // 3、封装userInfo
                UserInfo userInfo = userInfoApi.findByUserId(comment.getUserId());
                BeanUtil.copyProperties(userInfo, vo);
                // 4、封装comment
                vo.setContent(comment.getContent()); // 评论内容
                vo.setCreateDate(RelativeDateFormat.format(new Date(comment.getCreated()))); // 评论时间
                vo.setLikeCount(comment.getLikeCount()); // 评论点赞数量

                // 5、将vo添加到list
                voList.add(vo);
            }
        }

        // 6、将list添加到 pageBeanVo
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity publishComment(String publishId, String text) {
        // 1、封装comment对象
        Comment comment = new Comment();
        comment.setPublishId(new ObjectId(publishId)); // 动态id
        comment.setCommentType(2); // comment类型：评论
        comment.setPubType(1); // 动态类型：动态
        comment.setUserId(UserHolder.getUserId()); // 评论人
        comment.setContent(text); // 评论正文

        // 2、api保存comment对象
        commentApi.save(comment);


        // 使用mq发送大数据圈子消息
        quanziMQService.commentPublishMsg(publishId);

        return ResponseEntity.ok(null);
    }

    public ResponseEntity findUserCommentVoByPage(Integer pageNum, Integer pageSize, Integer commentType) {

        // 1、调用api分页查询
        PageBeanVo pageBeanVo = commentApi.findCommentByPage(pageNum, pageSize, commentType, UserHolder.getUserId());

        // 2、封装UserCommentVo
        List<UserCommentVo> voList = new ArrayList<>();

        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();

        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                UserCommentVo vo = new UserCommentVo();

                UserInfo userInfo = userInfoApi.findByUserId(comment.getUserId());
                BeanUtil.copyProperties(userInfo, vo); // 指定评论人的信息
                vo.setCreateDate(DateUtil.format(new Date(comment.getCreated()), "yyyy-MM-dd HH:mm"));
                voList.add(vo);
            }
        }

        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
